/**
 * 
 */
package vn.isolar.util.parse_kml;

/**
 * @author ipman
 *
 */
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class ParsePoint extends DefaultHandler {
	private ArrayList<Points> ListPoint = new ArrayList<Points>();
	//DS cac diem - dan duong
	private ArrayList<Points> linePoints = new ArrayList<Points>();
	private String stringLine = "";
	private Points aPoint;
	private boolean in_name = false;
	private boolean in_kml = false;
	private boolean in_placemark = false;
	private boolean in_address = false;
	private boolean in_latlon = false;
	private boolean in_linestring = false;

	public ParsePoint() {
	}

	public ArrayList<Points> getPoints() {
		return this.ListPoint;
	}

	public ArrayList<Points> getLinePoints() {
		this.setLinePoints(stringLine);
		return this.linePoints;
	}

	private void setLinePoints(String str) {
		linePoints.clear();
		String[] lngLat = str.split(" ");
		for (int i = 0; i < lngLat.length; i++) {
			this.aPoint = new Points();
			this.aPoint.setLatLon(lngLat[i]);
			linePoints.add(aPoint);
		}
		
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void startElement(String n, String l, String q, Attributes a)
			throws SAXException {
		if (l.equals("kml"))
		{
			this.in_kml = true;
		}
		if (l.equals("Placemark")) {
			in_placemark = true;
			this.aPoint = new Points();
		}
		if (l.equals("name")) {
			this.in_name = true;
		}
		if (l.equals("address")) {
			this.in_address = true;
		}
		if (l.equals("coordinates")) {
			this.in_latlon = true;
		}
		if (l.equals("LineString")) {
			this.in_linestring = true;
		}
	}

	@Override
	public void endElement(String n, String l, String q) throws SAXException {
		if (l.equals("kml"))
		{
			this.in_kml = false;
		}
		if (l.equals("Placemark")) {
			in_placemark = false;
			this.ListPoint.add(aPoint);
		}
		if (l.equals("name")) {
			in_name = false;
		}
		if (l.equals("address")) {
			in_address = false;
		}
		if (l.equals("coordinates")) {
			in_latlon = false;
		}
		if (l.equals("LineString")) {
			this.in_linestring = false;
		}
	}

	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_name && this.in_placemark) {
			this.aPoint.setName(new String(ch, start, length));
		}
		if (this.in_address && this.in_placemark) {
			this.aPoint.setAddress(new String(ch, start, length));
		}
		if (this.in_latlon && this.in_placemark && !this.in_linestring) {
			this.aPoint.setLatLon(new String(ch, start, length));
		}
		//co the bi gioi han 4096 ky
		if (this.in_linestring) {
			stringLine = stringLine + new String(ch, start, length);
		}
	}
}
